package com.wowangz.cms.systemmanage.sparticipantbank.api;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wowangz.cms.common.pojo.AjaxDataEntity;
import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.component.datagrid.pojo.DataGrid;
import com.wowangz.cms.component.datagrid.pojo.DataResult;
import com.wowangz.cms.component.datagrid.pojo.DataStyle;
import com.wowangz.cms.component.datagrid.pojo.PangingParam;
import com.wowangz.cms.component.datagrid.utils.DataTypeConstant;
import com.wowangz.cms.component.datagrid.utils.PangingTools;
import com.wowangz.cms.syscore.IssTransactionContainer;
import com.wowangz.cms.syscore.api.BaseController;
import com.wowangz.cms.syscore.api.JSONFactory;
import com.wowangz.cms.syscore.utils.MessageResourcesUtils;
import com.wowangz.cms.syscore.utils.ServletUtils;
import com.wowangz.cms.systemmanage.city.pojo.City;
import com.wowangz.cms.systemmanage.city.service.CityService;
import com.wowangz.cms.systemmanage.province.pojo.Province;
import com.wowangz.cms.systemmanage.province.service.ProvinceService;
import com.wowangz.cms.systemmanage.sparticipantbank.pojo.Sparticipantbank;
import com.wowangz.cms.systemmanage.sparticipantbank.service.SparticipantbankService;

@Scope("prototype")
@Controller
public class SparticipantbankController extends BaseController{
	/**
	 * 点击菜单"支行信息管理"显示“支行信息管理”列表
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/systemmanage/sparticipantbank/querySparticipantbankList.web")
	public String querySparticipantbankList(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		return "/WEB-INF/jsp/systemmanage/sparticipantbank/sparticipantbankList";
	}
	
	/**
	 * 打开“支行信息管理”列表加载列表数据
	 * @param request
	 * @param response
	 * @param model
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/systemmanage/sparticipantbank/querySparticipantbankListInfoDataGrid.json")
	public void querySparticipantbankListInfoDataGrid(HttpServletRequest request, HttpServletResponse response, Model model) {

		PangingParam pangingParam = null;
		DataResult dataResult = new DataResult();
		DataGrid dataGrid = null;

		try {
			pangingParam = (PangingParam) ServletUtils.getParametersToBaseEntity(request, PangingParam.class);
			HashMap<String, Object> map = (HashMap) ServletUtils.getParametersToHashMap(request);
			dataGrid = new DataGrid();
			dataGrid.setPangingParam(pangingParam);

			map.put("startrow", dataGrid.getStartrow());
			map.put("endrow", dataGrid.getEndrow());
			map.put("rows", dataGrid.getRows());
			map.put("sort", dataGrid.getSort());
			map.put("order", dataGrid.getOrder());
			map.put("statusid", SysConstant.RecordStatus.VALID);

			SparticipantbankService sparticipantbankService = (SparticipantbankService) IssTransactionContainer.getInstance(SparticipantbankService.class, false);
			long resultCount = sparticipantbankService.findResultCount(map);
			List<Sparticipantbank> resultList = sparticipantbankService.findResultList(map);

			DataStyle dataStyle = new DataStyle();
			dataStyle.setPageIndex(pangingParam.getPage());
			dataStyle.setDataCount(resultCount);
			dataStyle.setDataList(resultList);

			dataStyle.addDepict("id", DataTypeConstant.LONG);
			dataStyle.addDepict("bankcode", DataTypeConstant.STRING);
			dataStyle.addDepict("bankfullname", DataTypeConstant.STRING);
			dataStyle.addDepict("sparticipantbankno", DataTypeConstant.STRING);
			dataStyle.addDepict("sparticipantfullname", DataTypeConstant.STRING);
			dataResult = PangingTools.buildResultPagerInfo(dataStyle);

			if (pangingParam.getReportExportType() == 0) {
				response.getWriter().write(JSONFactory.toJSONString(dataResult, userContext));
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 跳转支行信息添加页面
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/systemmanage/sparticipantbank/toAddSparticipantbank.web")
	public String toAddSparticipantbank(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		return "/WEB-INF/jsp/systemmanage/sparticipantbank/sparticipantbankAdd";
	}
	
	/**
	 * 新增前验证
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping(value = "/systemmanage/sparticipantbank/validattionSparticipantbank.json", method = { RequestMethod.GET, RequestMethod.POST })
	public void validattionSparticipantbank(HttpServletRequest request, HttpServletResponse response, Model model) {
		AjaxDataEntity ajaxDataEntity = new AjaxDataEntity();
		try {
			Sparticipantbank sparticipantbank = null;
			StringBuffer errorMsg = new StringBuffer();
			HashMap<String, Object> map = new HashMap<String, Object>();
			sparticipantbank = (Sparticipantbank) ServletUtils.getParametersToBaseEntity(request, Sparticipantbank.class);
			
			SparticipantbankService sparticipantbankService = (SparticipantbankService) IssTransactionContainer.getInstance(SparticipantbankService.class, false);
			map.put("statusid", SysConstant.RecordStatus.VALID);
			if(null != sparticipantbank.getId()){
				map.put("id", sparticipantbank.getId());
			}
			map.put("sparticipantbankno", sparticipantbank.getSparticipantbankno());
			if(!sparticipantbankService.validationSparticipantbank(map)){
				errorMsg.append(MessageResourcesUtils.getMessage(request, "systemmanage.sparticipant.sparticipantList.alert2"));
			}
			map.remove("sparticipantbankno");
			map.put("sparticipantfullname", sparticipantbank.getSparticipantfullname());
			if(!sparticipantbankService.validationSparticipantbank(map)){
				errorMsg.append(MessageResourcesUtils.getMessage(request, "systemmanage.sparticipant.sparticipantList.alert1"));
			}
			map.remove("sparticipantfullname");
			
			if( sparticipantbank.getBankid() == null ){
				errorMsg.append(MessageResourcesUtils.getMessage(request, "systemmanage.sparticipant.sparticipantList.alert3"));
			}else{
				map.put("bankid", sparticipantbank.getBankid());
				if(sparticipantbankService.validationHeadBank(map)){
					errorMsg.append(MessageResourcesUtils.getMessage(request, "systemmanage.sparticipant.sparticipantList.alert3"));
				}
			}
			
			map.remove("bankid");
			
			if(StringUtils.isNotEmpty(errorMsg.toString())){
				userContext.setErrorMessage(errorMsg.toString());
			}
			response.getWriter().write(JSONFactory.toJSONString(ajaxDataEntity, userContext));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 新增
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/systemmanage/sparticipantbank/addSparticipantbank.web", method = { RequestMethod.GET, RequestMethod.POST })
	public String addSparticipantbank(HttpServletRequest request, HttpServletResponse response, Model model) {
		Sparticipantbank sparticipantbank = null;
		try {
			sparticipantbank = (Sparticipantbank) ServletUtils.getParametersToBaseEntity(request, Sparticipantbank.class);
			sparticipantbank.setInputtime(new Date());
			sparticipantbank.setInputuserid(userContext.getLoginUser().getId());
			sparticipantbank.setModifytime(new Date());
			sparticipantbank.setModifyuserid(userContext.getLoginUser().getId());
			sparticipantbank.setStatusid(SysConstant.RecordStatus.VALID);
			SparticipantbankService sparticipantbankService = (SparticipantbankService) IssTransactionContainer.getInstance(SparticipantbankService.class, true);
			
			sparticipantbankService.add(sparticipantbank);
			userContext.setInfoMessage(MessageResourcesUtils.getMessage(request, "alertinfo.common.java.alert1"));
			userContext.setCloseWindow(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/WEB-INF/jsp/systemmanage/sparticipantbank/sparticipantbankAdd";
	}
	
	/**
	 * 跳转修改支行信息页面
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/systemmanage/sparticipantbank/toUpdateSparticipantbank.web")
	public String toUpdateSparticipantbank(HttpServletRequest request, HttpServletResponse response, Model model) {
		try {
			String sparticipantbankId = getRequest().getParameter("id");
			SparticipantbankService sparticipantbankService = (SparticipantbankService) IssTransactionContainer.getInstance(SparticipantbankService.class, false);
			Sparticipantbank sparticipantbank = sparticipantbankService.findSparticipantbankById(sparticipantbankId);
			
			model.addAttribute("sparticipantbank", sparticipantbank);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/WEB-INF/jsp/systemmanage/sparticipantbank/sparticipantbankUpdate";
	}
	
	/**
	 * 修改支行信息
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/systemmanage/sparticipantbank/updateSparticipantbank.web", method = { RequestMethod.GET, RequestMethod.POST })
	public String updateSparticipantbank(HttpServletRequest request, HttpServletResponse response, Model model) {
		Sparticipantbank sparticipantbank = null;
		try {
			sparticipantbank = (Sparticipantbank) ServletUtils.getParametersToBaseEntity(request, Sparticipantbank.class);
			sparticipantbank.setModifytime(new Date());
			SparticipantbankService sparticipantbankService = (SparticipantbankService) IssTransactionContainer.getInstance(SparticipantbankService.class, true);
			sparticipantbankService.update(sparticipantbank);
			userContext.setInfoMessage(MessageResourcesUtils.getMessage(request, "alertinfo.common.java.alert1"));
			userContext.setCloseWindow(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/WEB-INF/jsp/systemmanage/sparticipantbank/sparticipantbankUpdate";
	}
	
	/**
	 * 删除支行信息
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping(value = "/systemmanage/sparticipantbank/deleteSparticipantbank.json", method = { RequestMethod.GET, RequestMethod.POST })
	public void deleteSparticipantbank(HttpServletRequest request, HttpServletResponse response, Model model) {
		AjaxDataEntity ajaxDataEntity = new AjaxDataEntity();
		try {
			Long sparticipantbankId = Long.valueOf(getRequest().getParameter("id"));
			Sparticipantbank sparticipantbank = new Sparticipantbank();
			sparticipantbank.setId(sparticipantbankId);
			sparticipantbank.setStatusid(SysConstant.RecordStatus.INVALID);
			SparticipantbankService sparticipantbankService = (SparticipantbankService) IssTransactionContainer.getInstance(SparticipantbankService.class, false);
			sparticipantbankService.update(sparticipantbank);
			userContext.setInfoMessage(MessageResourcesUtils.getMessage(request, "alertinfo.common.java.alert3"));
			response.getWriter().write(JSONFactory.toJSONString(ajaxDataEntity, userContext));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
